package edu.fdzc.learn.controller;

import edu.fdzc.learn.entity.ExamSubmission;
import edu.fdzc.learn.entity.UserAnswer;
import edu.fdzc.learn.entity.vo.ExamResultVO;
import edu.fdzc.learn.entity.vo.ExamSubmissionVO;
import edu.fdzc.learn.service.ExamSubmissionService;
import edu.fdzc.learn.utils.ResultVO;
import edu.fdzc.learn.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 考试提交控制器
 */
@Slf4j
@RestController
@RequestMapping("/exam/submission")
public class ExamSubmissionController {

    @Autowired
    private ExamSubmissionService examSubmissionService;

    /**
     * 提交考试答案
     */
    @PostMapping("/submit")
    public ResultVO<?> submitExam(@RequestBody ExamSubmissionVO submissionVO) {
        try {
            Long submissionId = examSubmissionService.submitExam(submissionVO);
            return submissionId != null ?
                    ResultVOUtil.success("提交成功，提交记录 ID: " + submissionId) :
                    ResultVOUtil.error(500, "提交失败");
        } catch (Exception e) {
            log.error("考试提交失败: {}", e.getMessage());
            return ResultVOUtil.error(500, "系统错误: " + e.getMessage());
        }
    }

    /**
     * 获取考试结果
     */
    @GetMapping("/result/{submissionId}")
    public ResultVO<ExamResultVO> getResult(@PathVariable Long submissionId) {
        try {
            ExamResultVO resultVO = examSubmissionService.getExamResult(submissionId);
            return resultVO != null ?
                    ResultVOUtil.success(resultVO) :
                    ResultVOUtil.error(404, "未找到提交记录");
        } catch (Exception e) {
            log.error("获取考试结果失败: {}", e.getMessage());
            return ResultVOUtil.error(500, "系统错误: " + e.getMessage());
        }
    }

    /**
     * 批阅试卷（后台计算分数）
     */
    @PostMapping("/grade/{submissionId}")
    public ResultVO<ExamResultVO> gradeExam(@PathVariable Long submissionId) {
        try {
            // 先获取提交记录
            ExamSubmission submission = examSubmissionService.getSubmissionById(submissionId);
            if (submission == null) {
                return ResultVOUtil.error(404, "未找到提交记录");
            }
            // 解析答案
            List<UserAnswer> userAnswers = parseAnswers(submission.getAnswers());
            // 计算分数
            Integer score = examSubmissionService.calculateScore(submission.getExamId(), userAnswers);
            // 更新分数
            submission.setScore(score);
            examSubmissionService.updateById(submission);
            // 获取考试结果
            ExamResultVO resultVO = examSubmissionService.getExamResult(submissionId);
            return ResultVOUtil.success(resultVO);
        } catch (Exception e) {
            log.error("批阅试卷失败: {}", e.getMessage());
            return ResultVOUtil.error(500, "批阅失败: " + e.getMessage());
        }
    }

    // 解析答案的方法，需要根据实际情况实现
    private List<UserAnswer> parseAnswers(String answers) {
        // 这里只是示例，需要根据实际的 JSON 格式解析
        return new ArrayList<>();
    }
}